import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * 管理用户
 * @author lys
 */

public class ManageUser {
    private static ManageUser instance;
    public JPanel jPanel = new JPanel();
    public JLayeredPane jLayeredPane = new JLayeredPane();

    private JLabel jLabel = new JLabel("用户管理");
    private JLabel jLabel2 = new JLabel("全部用户信息展示：");
    private JLabel jLabel3= new JLabel("请输入用户名");
    private JTextField field = new JTextField(25);
    public DefaultTableModel model = new DefaultTableModel();
    private Font font = new Font("宋体", Font.BOLD, 60);
    private Font font1 = new Font("宋体", Font.BOLD, 25);
    private Font font2 = new Font("宋体", Font.BOLD, 20);

    private JButton jButton = new JButton("管理用户");
    private JTable jTable;

    private ManageUser() {
        Icon i = new ImageIcon(ManageUser.class.getResource("img/6.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 1200, 800);

        jLabel.setFont(font);
        jLabel.setBounds(485, 35, 800, 100);
        jLabel.setForeground(Color.white);

        jLabel2.setFont(font1);
        jLabel2.setBounds(200, 180, 250, 50);
        jLabel2.setForeground(Color.white);

        jButton.setFont(font1);
        jButton.setBounds(850, 180, 150, 50);
        jButton.setForeground(Color.white);
        jButton.setBackground(Color.white);
        jButton.setOpaque(false);

        model.addColumn("用户名");
        model.addColumn("姓名");
        model.addColumn("学号");
        model.addColumn("是否被封禁");
        jTable = new JTable(model);
        jTable.setOpaque(false);

        JScrollPane pane = new JScrollPane(jTable);
        pane.setBounds(200, 250, 800, 400);
        pane.setOpaque(false);



//		for(int k = 0; k < 30; k++) {
//			model.addRow(new Vector<Integer>());
//		}
        // FindBook.allbook(model);

        JTableHeader head = jTable.getTableHeader();
        // 设置表头的大小
        head.setPreferredSize(new Dimension(head.getWidth(), 30));
        // 设置表头字体大小
        head.setFont(new Font("宋体", Font.BOLD, 20));
        // head.setForeground(Color.cyan);
        head.setBackground(Color.cyan);
        // 设置表格的行宽
        jTable.setRowHeight(30);
        // 设置表格行中字体大小
        jTable.setFont(new Font("宋体", Font.ROMAN_BASELINE, 17));

        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        jTable.setDefaultRenderer(Object.class, renderer);


        jLayeredPane.add(jLabel,new Integer(100),1);
        jLayeredPane.add(jLabel2,new Integer(100),2);
        jLayeredPane.add(jButton,new Integer(100),3);
        jLayeredPane.add(pane,new Integer(100),4);
        jLayeredPane.add(Label,new Integer(0),0);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AdminInterface.getInstance().admin.setVisible(false);
                CloseUser.getInstance().setVisible(true);
            }
        });

        showUsers();

        jButton.setFocusPainted(false);
    }

    public static ManageUser getInstance() {
        if(instance == null) instance = new ManageUser();
        return instance;
    }
    /**
     * @author zj
     * @return 获取所有用户信息
     */
    public ArrayList<User> getUsers(){
        ResultSet rs;
        String sql = "select * from user";
        ArrayList<User> users = new ArrayList<>();
        try{
            PreparedStatement preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
            rs = preSql.executeQuery();
            while(rs.next()){
                String name = rs.getString("name");
                String sid = rs.getString("sid");
                String username = rs.getString("username");
                String password = rs.getString("password");
                Boolean isClosed = rs.getBoolean("isclosed");
                User user = new User(name, sid, username, password, isClosed);
                users.add(user);
            }
        }catch (Exception e){
            System.out.println(e);
        }
        return users;
    }
    public static void removeInstance(){
        instance = null;
    }

    /**
     * 展示所有用户信息
     */
    public void showUsers(){
        ArrayList<User> users = getUsers();
        DefaultTableModel model2 = (DefaultTableModel) jTable.getModel();
        model2.getDataVector().clear();
        model2.fireTableDataChanged();
        jTable.updateUI();
        for(User user: users){
            String[] column = {user.getUsername(), user.getName(), user.getSid(), ""};
            if(user.getClosed()){
                column[3] = "封禁";
            }
            else{
                column[3] = "活跃";
            }
            model2.addRow(column);
        }
    }
}
